package com.qf.schedule;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.entity.TbSpeedkill;
import com.qf.mapper.SpeedkillMapper;
import com.qf.util.DateUtil;
import com.qf.util.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class LoadSpeedkillGoods {

    @Autowired
    SpeedkillMapper speedkillMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Scheduled(cron = "0/5 * * * * ? ") //每隔5秒钟执行一次任务
    public void load() {
        //查询
        //查询是时间段
        List<Date> dateMenus = DateUtil.getDateMenus();
        for (Date start : dateMenus) {//10:00
            QueryWrapper<TbSpeedkill> wrapper = new QueryWrapper<>();
            wrapper.ge("speed_starttime", start);//起始时间条件，大于
            wrapper.le("speed_endtime", DateUtil.addDateHour(start, 2));//小于
            wrapper.eq("speed_checkstatus", 1);//秒杀的商品需要审核通过，状态为1
            //条件：这些商品不在redis中存在？？？？？
            Set set = redisTemplate.opsForHash().keys(RedisKey.SPEED_KILL_GOODS + DateUtil.date2Str(start));
            if(set!=null&& set.size()>0)
                wrapper.notIn("goods_id",set);//3 4 5

            List<TbSpeedkill> tbSpeedkills = speedkillMapper.selectList(wrapper);//3 4 5
            for (TbSpeedkill tbSpeedkill : tbSpeedkills) {
                //redis中的时间段内的秒杀商品/库存
                redisTemplate.boundHashOps(RedisKey.SPEED_KILL_GOODS + DateUtil.date2Str(start)).put(tbSpeedkill.getGoodsId().toString(), tbSpeedkill);

                //库存--tbSpeedkill--speedStock属性
                redisTemplate.boundValueOps(RedisKey.SPEED_KILL_GOODSSTOCK+tbSpeedkill.getGoodsId().toString()).set(tbSpeedkill.getSpeedStock());
            }

        }

        //redis中是否存在指定的key
        //查库
    }
}
